var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

// 陈茹琳
// 后台管理
var selectNewsRouter = require('./routes/selectNews');
var addNewsRouter = require('./routes/addNews');
var newsDetailRouter = require('./routes/newsDetail');
var updateNewsRouter = require('./routes/updateNews');
var delNewsRouter = require('./routes/delNews');
// 移动端
var selectEatRouter = require('./routes/selectEat');
var eatDetailsRouter = require('./routes/eatDetails');
var ownerLoginRouter = require('./routes/ownerLogin');

// 乔月
// 后台管理
var selectExpressRouter = require('./routes/selectExpress');
var addExpressRouter = require('./routes/addExpress');
var changeExpressRouter = require('./routes/changeExpress');
var detailExpressRouter = require('./routes/detailExpress');
var delExpressRouter = require('./routes/delExpress');
// 移动端
var hotspotRouter = require('./routes/hotspot');
var hotspotDetailRouter = require('./routes/hotspotDetail');

// 张文静
var loginRouter = require('./routes/login');
var addStaffRouter = require('./routes/addStaff');
var selectStaffRouter = require('./routes/selectStaff');
var selectallStaffRouter = require('./routes/selectallStaff');
var delStaffRouter = require('./routes/delStaff');
var staffDetail = require('./routes/staffDetail');
var changeStaff = require('./routes/changeStaff');

// 苏林
var selectownerRouter = require('./routes/selectowner');
var addownerRouter = require('./routes/addowner');
var delownerRouter = require('./routes/delowner');
var ownerDetailRouter = require('./routes/ownerDetail');
var changeownerRouter = require('./routes/changeowner');

// 张文静
// 报修
var selectRepairRouter = require('./routes/selectRepair');
var addRepairRouter = require('./routes/addRepair');
var delRepairRouter = require('./routes/delRepair');
var repairDetail = require('./routes/repairDetail');
var changeRepair = require('./routes/changeRepair');

// 吕爻
var selectUserRouter = require('./routes/selectUser');
var addUserRouter = require('./routes/addUser');
var delUserRouter = require('./routes/delUser');
var userDetail = require('./routes/userDetail');
var changeUser = require('./routes/changeUser');

var app = express();

//允许跨域请求
app.all("*", (req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT");
  res.header("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,token");
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
})

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

app.use('/selectNews', selectNewsRouter);
app.use('/addNews', addNewsRouter);
app.use('/newsDetail', newsDetailRouter);
app.use('/updateNews', updateNewsRouter);
app.use('/delNews', delNewsRouter);
app.use('/selectEat', selectEatRouter);
app.use('/eatDetails', eatDetailsRouter);
app.use('/ownerLogin', ownerLoginRouter)

// 后台管理
app.use('/selectExpress',selectExpressRouter);
app.use('/addExpress',addExpressRouter);
app.use('/changeExpress',changeExpressRouter);
app.use('/detailExpress',detailExpressRouter);
app.use('/delExpress',delExpressRouter);
// 移动端
// app.use('/headLines',headLinesRouter);
// app.use('/headlineDetail',headlineDetailRouter);
app.use('/hotspot',hotspotRouter);
app.use('/hotspotDetail',hotspotDetailRouter);

app.use('/login', loginRouter);
app.use('/addStaff',addStaffRouter);
app.use('/selectStaff',selectStaffRouter);
app.use('/selectallStaff',selectallStaffRouter);
app.use('/delStaff', delStaffRouter);
app.use('/staffDetail', staffDetail);
app.use('/changeStaff', changeStaff);


app.use('/selectowner',selectownerRouter);
app.use('/addowner',addownerRouter);
app.use('/delowner',delownerRouter);
app.use('/ownerDetail',ownerDetailRouter);
app.use('/changeowner',changeownerRouter);
// 报修
app.use('/selectRepair', selectRepairRouter);
app.use('/addRepair',addRepairRouter);
app.use('/delRepair',delRepairRouter);
app.use('/repairDetail', repairDetail);
app.use('/changeRepair', changeRepair);


app.use('/selectUser',selectUserRouter);
app.use('/addUser',addUserRouter);
app.use('/delUser',delUserRouter);
app.use('/userDetail',userDetail);
app.use('/changeUser',changeUser);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
